Skip to content

Refine bash completion option handling and context awareness#3773

Draft
PeterDaveHello wants to merge 1 commit intonvm-sh:masterfrom
PeterDaveHello:improve/bash-completion-options
Draft

Refine bash completion option handling and context awareness#3773
PeterDaveHello wants to merge 1 commit intonvm-sh:masterfrom
PeterDaveHello:improve/bash-completion-options

Conversation

@PeterDaveHello
Copy link
Copy Markdown
Collaborator

Make option suggestions context-aware by subcommand and argument position to avoid invalid combinations.
Avoid stale or duplicate suggestions (including value flags and glob-like aliases) while preserving current-word completion.
Align option lists with nvm --help and observed CLI behavior for run/exec/ls/uninstall.

GitHub Copilot PR Summary:

This pull request significantly improves the Bash completion logic for the nvm command, making completions more context-aware and robust. The changes refactor how options and arguments are parsed, provide more accurate suggestions based on subcommands and flags, and handle edge cases for specific commands.

Improvements to Bash completion logic:

  • Enhanced the __nvm_generate_completion function to filter out options that have already been used in the command line, preventing duplicate suggestions.
  • Refactored the __nvm_options function to accurately determine the current subcommand and argument state, enabling context-sensitive option suggestions for each subcommand and handling special cases like install and ls.

Context-aware completions for subcommands:

  • Updated the main __nvm completion function to parse the current subcommand and argument count, providing relevant completions for subcommands like use, run, exec, ls, list, uninstall, alias, and unalias, and handling special flags such as --lts and --no-alias.
  • Improved handling of installed node versions and aliases for completions, including error suppression for nvm_ls.

These changes make the tab-completion experience for nvm users more accurate and user-friendly by only suggesting relevant options and arguments based on the current command context.

Make option suggestions context-aware by subcommand and argument
position to avoid invalid combinations.
Avoid stale or duplicate suggestions (including value flags and
glob-like aliases) while preserving current-word completion.
Align option lists with nvm --help and observed CLI behavior for
run/exec/ls/uninstall.
@dosubot
Copy link
Copy Markdown

dosubot bot commented Feb 1, 2026

Related Documentation

No published documentation to review for changes on this repository.

Write your first living document

How did I do? Any feedback?  Join Discord

@caroguillermo717-png

This comment was marked as spam.

-*) __nvm_options; return 0 ;;
esac

for (( i=1; i < COMP_CWORD; i++ )); do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subcommand/argument parsing loop here (lines 247-281) is nearly identical to the one in __nvm_options() (lines 112-138). This is ~60 lines of duplicated logic that will need to be updated in two places whenever the parsing rules change.

Please extract this into a shared helper function that sets shell variables (e.g., subcommand, has_arg, arg_count, skip_next, etc.) and call it from both __nvm_options() and __nvm().

if [ "${found_subcommand}" -eq 1 ] && [ "${has_arg}" -eq 1 ] && [ "${allow_after_arg}" -ne 1 ] && [ "${install_post_arg}" -ne 1 ]; then
OPTIONS=''
else
case "${subcommand}" in
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These per-subcommand option lists will silently become stale whenever nvm adds or removes flags. We need a test that validates that bash_completion is updated when nvm's arg list changes - eg, a test that parses the case statement in nvm() for each subcommand's accepted flags and compares them against the options listed here, or better, the help output.

noglob_set=0
case "$-" in
*f*) noglob_set=1 ;;
esac
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This O(n×m) filtering (all options × all COMP_WORDS) is unnecessary - compgen -W already handles prefix matching and deduplication against the current word. The only case this helps is suppressing options that were already used earlier in the command line, but that's not typically how bash completions work (most tools allow repeating flags). If this behavior is desired, it should be documented as intentional, but I'd suggest dropping it to keep things simple.

if [ "${install_post_arg}" -eq 1 ]; then
OPTIONS='--reinstall-packages-from= --skip-default-packages'
else
OPTIONS='-s -b -w -j --reinstall-packages-from= --lts --lts= --skip-default-packages --latest-npm --no-progress --alias= --default --save'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing --offline flag (and likely others as new flags are added over time - see comment about staleness above).

;;
esac

case "${previous_word}" in
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case "${previous_word}" block is now unreachable - the new case "${subcommand}" block above (lines 304-327) handles all the same subcommands and returns early. This dead code should be removed.

@ljharb ljharb marked this pull request as draft March 26, 2026 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants